package com.shenhaofeng.app.adapter;

import android.support.v4.widget.Space;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;


/**
 * 有头脚的适配器
 * Created by Administrator on 2016/7/20.
 */
public abstract class HeaderFooterAdapter<F, H> extends ModuleAdapter {

    public static final int TYPE_HEADER = -1;

    public static final int TYPE_FOOTER = -2;

    private Module<H> headerModule = Module.createModule(TYPE_HEADER);

    private Module<F> footerModule = Module.createModule(TYPE_FOOTER);


    /**
     * 默认构造方法
     */
    public HeaderFooterAdapter() {
        _addModule(headerModule);
        _addModule(footerModule);
    }


    /**
     * 尽量不要调用该方法
     *
     * @return
     */
    @Deprecated
    @Override
    public ModuleManager getModuleManager() {
        return super.getModuleManager();
    }


    @Override
    public void addModule(Module module) {
        int count = getModuleManager().getModuleCount();
        addModule(count - 1, module);
    }


    @Override
    public void removeModule(Module module) {
        //禁止移除头部和脚部的Module
        if (module == headerModule || module == footerModule) {
            return;
        }
        super.removeModule(module);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case TYPE_HEADER: {
                onBindHeadViewHolder(holder, position);
                break;
            }
            case TYPE_FOOTER: {
                onBindFootViewHolder(holder, position);
                break;
            }
            default: {
                onBindItemViewHolder(holder, position);
                break;
            }
        }
    }


    /**
     * 绑定头部
     *
     * @param holder
     * @param position
     */
    protected void onBindHeadViewHolder(RecyclerView.ViewHolder holder, int position) {

    }


    /**
     * 绑定页脚
     *
     * @param holder
     * @param position
     */
    protected void onBindFootViewHolder(RecyclerView.ViewHolder holder, int position) {

    }


    protected abstract void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position);


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_HEADER: {
                return onCreateHeadViewHolder(parent);
            }
            case TYPE_FOOTER: {
                return onCreateFootViewHolder(parent);
            }
            default: {
                return onCreateItemViewHolder(parent, viewType);
            }
        }

    }


    public abstract RecyclerView.ViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType);


    /**
     * 创建头部,如果需要可以重写该方法和{@link HeaderFooterAdapter#onBindHeadViewHolder(RecyclerView.ViewHolder, int)}
     *
     * @param parent
     * @return
     */
    protected RecyclerView.ViewHolder onCreateHeadViewHolder(ViewGroup parent) {
        return new EmptyViewHolder(new Space(parent.getContext()));
    }


    /**
     * 创建页脚,如果需要可以重写该方法和{@link HeaderFooterAdapter#onBindFootViewHolder(RecyclerView.ViewHolder, int)}
     *
     * @param parent
     * @return
     */
    protected RecyclerView.ViewHolder onCreateFootViewHolder(ViewGroup parent) {
        return new EmptyViewHolder(new Space(parent.getContext()));
    }


    /**
     * 添加module,仅供内部使用
     *
     * @param module
     */
    private void _addModule(Module module) {
        super.addModule(module);
    }


    /**
     * 获取页脚Module
     *
     * @return
     */
    public Module<F> getFooterModule() {
        return footerModule;
    }


    /**
     * 获取页头Module
     *
     * @return
     */
    public Module<H> getHeaderModule() {
        return headerModule;
    }


    /**
     * 默认的页头页脚
     */
    class EmptyViewHolder extends RecyclerView.ViewHolder {

        public EmptyViewHolder(View itemView) {
            super(itemView);
        }
    }
}
